package com.matt.repository.impl;

import com.matt.domain.course.CoursePlan;
import com.matt.foundation.DO.CoursePlanDO;
import com.matt.foundation.constant.CourseConstant;
import com.matt.foundation.query.AreaCourseQuery;
import com.matt.foundation.query.CoursePlanQuery;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.CoursePlanTeachingAreaRepository;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/19
 * *******************************
 */
@Component
public class CoursePlanTeachingAreaRepositoryImpl extends BaseNativeSqlRepository implements CoursePlanTeachingAreaRepository {

    @Override
    public List<CoursePlanDO> findCoursePlanByCondition(CoursePlanQuery query) {
        String querySql = String.format("select ce.type as templateType,cp.id as id, cp.courseDate as courseDate, cp.beginTime as beginTime, cp.endTime as endTime, cp.courseName as courseName, cp.prepareStatus as prepareStatus, cp.courseStatus as courseStatus, " +
                "  cp.courseType as courseType,  cp.typeName as typeName, ce.id as courseId, c.name as classesName  " +
                " from course_plan_classes cp, classes c,tc_vendor_course ce   WHERE cp.classesId = c.id and ce.id=cp.courseId and  cp.category = '%s' ", CourseConstant.Category.EARLY.codeOf());

        if (StringUtils.isNotBlank(query.getBeginDate())) {
            querySql += String.format(" and cp.courseDate >= '%s' ", query.getBeginDate());
        }
        if (StringUtils.isNotBlank(query.getEndDate())) {
            querySql += String.format(" and cp.courseDate <= '%s' ", query.getEndDate());
        }
        if (Objects.nonNull(query.getPrepareStatus())) {
            querySql += String.format(" and cp.prepareStatus = '%s' ", query.getPrepareStatus());
        }
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and cp.areaId = '%s' ", query.getAreaId());
        }

        querySql += " order by cp.courseDate,cp.endTime  asc " ;
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());

        List<CoursePlanDO> objectArrayList = sqlObjectList(querySql, CoursePlanDO.class);
        return objectArrayList;
    }

    @Override
    public Long countCoursePlanByCondition(CoursePlanQuery query) {
        String querySql = String.format("select count(cp.id) " +
                " from course_plan cp WHERE cp.category = '%s' ", CourseConstant.Category.EARLY.codeOf());

        if (StringUtils.isNotBlank(query.getBeginDate())) {
            querySql += String.format(" and cp.courseDate >= '%s' ", query.getBeginDate());
        }
        if (StringUtils.isNotBlank(query.getEndDate())) {
            querySql += String.format(" and cp.courseDate <= '%s' ", query.getEndDate());
        }
        if (Objects.nonNull(query.getPrepareStatus())) {
            querySql += String.format(" and cp.prepareStatus = '%s' ", query.getPrepareStatus());
        }
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and cp.areaId = '%s' ", query.getAreaId());
        }
        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }


    @Override
    public List<Object[]> findCoursePlanByAreaId(AreaCourseQuery query) {
        String querySql = String.format("select cp.id, cp.courseDate, cp.beginTime, cp.endTime, cp.courseName, cp.prepareStatus, a.name, cp.teacherId " +
                " from course_plan cp LEFT join teaching_area a on cp.teachingAreaId = a.id WHERE true ");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and cp.areaId = '%s' ", query.getAreaId());
        }
        if (StringUtils.isNotBlank(query.getTodayBegin())) {
            querySql += String.format(" and cp.courseDate >= '%s' ", query.getTodayBegin());
        }
        if (Objects.nonNull(query.getPrepareStatus())) {
            querySql += String.format(" and cp.prepareStatus = '%s' ", query.getPrepareStatus());
        }
        querySql += " order by cp.courseDate asc " ;
        querySql += String.format(" limit %s, %s ", query.getPageNum() * query.getPageSize() ,  query.getPageSize());

        List<Object[]> objectArrayList = sqlArrayList(querySql);
        return objectArrayList;
    }

    @Override
    public Long countCoursePlanByAreaId(AreaCourseQuery query) {
        String querySql = String.format("select count(cp.id) " +
                " from course_plan cp LEFT join teaching_area a on cp.teachingAreaId = a.id WHERE true ");
        if (StringUtils.isNotBlank(query.getAreaId())) {
            querySql += String.format(" and cp.areaId = '%s' ", query.getAreaId());
        }
        if (StringUtils.isNotBlank(query.getTodayBegin())) {
            querySql += String.format(" and cp.courseDate >= '%s' ", query.getTodayBegin());
        }
        if (Objects.nonNull(query.getPrepareStatus())) {
            querySql += String.format(" and cp.prepareStatus = '%s' ", query.getPrepareStatus());
        }
        List objectArrayList = sqlSingleList(querySql);
        return Long.valueOf(objectArrayList.get(0).toString());
    }

    @Override
    public List<Object[]> findCoursePlanListByAreaId(String areaId, String todayBegin) {
        String querySql = String.format("select cp.id, cp.courseDate, cp.beginTime, cp.endTime, a.id as aid, a.name, cp.teacherId,  cp.courseName, cp.courseType, cp.courseEarlyId, cp.typeName " +
                " from course_plan cp LEFT join teaching_area a on cp.teachingAreaId = a.id WHERE true ");
        if (StringUtils.isNotBlank(areaId)) {
            querySql += String.format(" and cp.areaId = '%s' ", areaId);
        }
        if (StringUtils.isNotBlank(todayBegin)) {
            querySql += String.format(" and cp.courseDate >= '%s' ", todayBegin);
        }
        querySql += " order by cp.courseDate asc " ;

        List<Object[]> objectArrayList = sqlArrayList(querySql);
        return objectArrayList;
    }
}
